﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Services;
using Icoders.MarketTracker.Web.Lib.Validation;

namespace Icoders.MarketTracker.Web.Areas.Admin.Controllers
{
    [CustomAuthorize(Roles = "ROLE_ADMIN")]
    public class PointOfSaleController : Controller
     {
         private IPointOfSaleRepository _pointOfSaleRepository;
         private IDropDownService _dropDownService;
         private IUserRepository _userRepository;
         private IAuditTrailRepository _auditTrailRepository;

        public PointOfSaleController(IPointOfSaleRepository pointOfSaleRepository, IDropDownService dropDownService, IUserRepository userRepository, IAuditTrailRepository auditTrailRepository)
        {
            _pointOfSaleRepository = pointOfSaleRepository;
            _dropDownService = dropDownService;
            _userRepository = userRepository;
            _auditTrailRepository = auditTrailRepository;
        }

        private void Bind()
         {
             ViewBag.ClientList = _dropDownService.GetAllClient();
         }

         public ActionResult Index()
         {
             return View(_pointOfSaleRepository.GetAll());
         }
         public ActionResult Create()
         {
             Bind();
             return View(new PointOfSale());
         }
         [HttpPost]
         public ActionResult Create(PointOfSale model)
         {
             try
             {
                 model.Code = "1234";
                 model.Description = model.Name;
                 _pointOfSaleRepository.Save(model);
                 Saveaudit("Added Point Of Sale", HttpContext.User.Identity.Name, ActionType.Added);
             }
             catch (DomainValidationException dve)
             {
                 ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                 return View(model);
             }
             catch (Exception ex)
             {
                 ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                 return View(model);
             }
             return RedirectToAction("Index");
         }
         public ActionResult Edit(int id)
         {
             Bind();
             PointOfSale pos = _pointOfSaleRepository.GetById(id);
             return View(pos);
         }
         [HttpPost]
         public ActionResult Edit(PointOfSale model)
         {
             try
             {
                 model.Description = model.Name;
                 _pointOfSaleRepository.Save(model);
                 Saveaudit("Edited Point Of Sale Type", HttpContext.User.Identity.Name, ActionType.Updated);
             }
             catch (DomainValidationException dve)
             {
                 ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                 return View(model);
             }
             catch (Exception ex)
             {
                 ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                 return View(model);
             }

             return RedirectToAction("Index");
         }
         public ActionResult Details(int id)
         {
             PointOfSale pointOfSale = new PointOfSale();
             pointOfSale = _pointOfSaleRepository.GetById(id);
             return View(pointOfSale);
         }
         public ActionResult Delete(int id)
         {
             _pointOfSaleRepository.Delete(id);
             Saveaudit("Deleted Point Of Sale Type", HttpContext.User.Identity.Name, ActionType.Deleted);
             return RedirectToAction("index");
         }
         public void Saveaudit(string description, string username, ActionType actionType)
         {
             //  private IUserRepository _userRepository;
             //private IAuditTrailRepository _auditTrailRepository;
             // Saveaudit("Changed Password", HttpContext.User.Identity.Name, ActionType.Updated);
             try
             {
                 User user = _userRepository.GetByUsername(username);
                 AuditTrail auditTrail = new AuditTrail();
                 auditTrail.Description = description;
                 auditTrail.UserId = user.Id;
                 auditTrail.ActionTypeId = Convert.ToInt32(actionType);
                 _auditTrailRepository.Save(auditTrail);
             }
             catch (Exception)
             {
                 throw;
             }
         }
     }
}
